package com.atguigu.edu.service.impl;

import com.atguigu.edu.entity.EduCourse;
import com.atguigu.edu.entity.EduCourseDescription;
import com.atguigu.edu.entity.vo.EduCourseVo;
import com.atguigu.edu.entity.vo.EduCoursePublishVo;
import com.atguigu.edu.mapper.EduCourseDescriptionMapper;
import com.atguigu.edu.mapper.EduCourseMapper;
import com.atguigu.edu.service.EduCourseDescriptionService;
import com.atguigu.edu.service.EduCourseService;
import com.atguigu.service.base.exception.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author alexzshl
 * @since 2021-05-04
 */
@Service
@Transactional
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduCourseDescriptionMapper courseDescriptionMapper;

    @Resource
    private EduCourseDescriptionService courseDescriptionService;

    @Override
    public EduCourseVo saveCourseInfo(EduCourseVo courseInfo) {
        EduCourse course = new EduCourse();
        EduCourseDescription courseDesc = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfo, course);
        BeanUtils.copyProperties(courseInfo, courseDesc);
        int insertCourse = baseMapper.insert(course);
        if (insertCourse <= 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }
        courseDesc.setId(course.getId());
        int insertCourseDescription = courseDescriptionMapper.insert(courseDesc);
        if (insertCourseDescription <= 0) {
            throw new GuliException(20001, "添加课程描述信息失败");
        }

        courseInfo.setId(course.getId());
        return courseInfo;
    }

    @Override
    public EduCourseVo getCourseInfo(String courseId) {
        EduCourseVo courseInfoVo = new EduCourseVo();

        EduCourse course = this.getById(courseId);
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);

        if (course != null) {
            BeanUtils.copyProperties(course, courseInfoVo);
        } else {
            return null;
        }
        if (courseDescription != null) {
            courseInfoVo.setDescription(courseDescription.getDescription());
        }

        return courseInfoVo;
    }

    @Override
    public Boolean updateCourseInfo(EduCourseVo courseInfo) {
        EduCourse course = new EduCourse();
        EduCourseDescription courseDesc = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfo, course);
        BeanUtils.copyProperties(courseInfo, courseDesc);

        int updateCourse = baseMapper.updateById(course);
        if (updateCourse <= 0) {
            throw new GuliException(20001, "更新课程信息失败");
        }

        courseDesc.setId(course.getId());
        int updateCourseDescription = courseDescriptionMapper.updateById(courseDesc);
        if (updateCourseDescription <= 0) {
            throw new GuliException(20001, "更新课程描述信息失败");
        }

        return true;
    }

    @Override
    public EduCoursePublishVo getEduCoursePublishVoById(String courseId) {
        EduCoursePublishVo eduCoursePublishInfoById = baseMapper.getEduCoursePublishInfoById(courseId);
        return eduCoursePublishInfoById;
    }

    @Override
    public List<EduCoursePublishVo> getCourseList() {
        return null;
    }

    @Override
    public List<EduCoursePublishVo> getCourseListPage(Integer current, Integer limit) {
        return null;
    }
}
